<?php

namespace app\user\logic\user;

use app\common\exception\LogicException;
use app\common\logic\Common;
use app\user\event\subscribes\user\Question as QuestionEvent;
use app\user\model\user\Question as QuestionModel;
use think\facade\Event;

/**
 * 消息通知
 */
class Question extends QuestionModel
{

    /**
     * 添加提问
     */
    public function addQuestion($param)
    {
        // 验证数据
        $this->getValidate()->scene('add')->failException()->check($param);
        // 参数
        $mid                   = static::$mid;
        $param['send_user_id'] = $mid;
        // 内容过滤
        $param['content'] = app(Common::class)->filterSensitive($param['content'], $param['recive_user_id'], 'user_question');
        // 附件图片
        $param['attach'] = !empty($param['attach']) ? array_map(function ($value) {
            return intval($value);
        }, explode(',', $param['attach'])) : null;

        // 添加
        $re = $this::create($param);
        // 判断及返回
        if ($re->isEmpty()) throw new LogicException("提问失败");
        // 事务处理消息 - 收到提问提醒
        $data['send_user_id']   = $mid;
        $data['recive_user_id'] = $param['recive_user_id'];
        $data['notify_data_id'] = $re->id;
        $data['content']        = $param['content'];
        $data['notify_type']    = 3;

        Event::subscribe(QuestionEvent::class);
        Event::trigger('QuestionNotify', $data);

        return $re;
    }

    /**
     * 添加提问回复
     */
    public function addQuestionReply($param)
    {
        // 验证数据
        $this->getValidate()->scene('addReply')->failException()->check($param);
        // 定义参数
        $mid              = request()->mid;
        $reply_mod        = app(QuestionReply::class);
        $param['user_id'] = $mid;
        // 内容过滤
        $param['content'] = app(Common::class)->filterSensitive($param['content'], $param['reply_user_id'], 'user_question_reply');
        // 附件图片
        $param['attach'] = !empty($param['attach']) ? array_map(function ($value) {
            return intval($value);
        }, explode(',', $param['attach'])) : null;
        // 不能回复自己
        if ($param['reply_user_id'] == $mid) throw new LogicException('不能回复自己');
        // 添加回复
        $reply = $reply_mod::create($param);

        if ($reply->isEmpty()) throw new LogicException("添加失败");

        // 用户信息
        // $reply = $reply_mod->with([
        //     'reply_user',
        //     'user.avatar_url'
        // ])->findOrEmpty($reply->id);

        // 事务处理消息 - 资讯点评回复提醒
        $data['send_user_id']   = $mid;
        $data['recive_user_id'] = $param['reply_user_id'];
        $data['notify_data_id'] = $param['question_id'];
        $data['content']        = $param['content'];
        $data['notify_type']    = 3;//6;

        Event::subscribe(QuestionEvent::class);
        Event::trigger('QuestionNotify', $data);

        // 返回数据
        return $reply;
    }

    /**
     * 获取用户提问对话框
     */
    public function getQuestionCommunication($id, $param)
    {
        // 分页
        $page  = $param['page'] ?? 1;
        $reply = app(QuestionReply::class)->field('user_id,content,attach,create_time')->with(['user.avatar_url'])->where('question_id', $id)->order('create_time', 'DESC')->paginate($this->getListRows())->hidden(['attach']);

        // 最后页
        $lastPage = $reply->lastPage();
        if ($lastPage == 0 || $lastPage == $page) {
            // 问题
            $question[0] = $this->field('content,send_user_id as user_id,attach,create_time')->with(['user.avatar_url'])->findOrFail($id)->hidden(['attach'])->toArray();
        }

        // 数据组装
        $data                 = [];
        $data['total']        = $reply->total();
        $data['per_page']     = $this->getListRows();
        $data['current_page'] = $reply->currentPage();
        $data['last_page']    = $lastPage;

        $reply2 = array_reverse($reply->all());

        // 合并数据
        if ($lastPage > 0 && $lastPage != $page) {
            $data['data'] = $reply2;
        } else {
            $data['data'] = array_merge($question, $reply2);
        }

        return $data;
    }
}
